package com.woniu.map.remote;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.woniu.internalcommon.constant.AmapConfigConstants;
import com.woniu.internalcommon.response.DirectionResponse;
import com.woniu.map.remote.url.AmapUrl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
 public class MapDirectionClient {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private AmapUrl amapUrl;
    /**
     * 格式化经纬度
     */


    /**
     * 调用高德地图API获取距离和时长
     */
 public DirectionResponse getDirection(String originLongitude,
                                       String originLatitude,
                                       String destLongitude,
                                       String destLatitude) {
     // 构建URL

     String drivingUrl =amapUrl.buildDrivingUrl(originLongitude, originLatitude, destLongitude, destLatitude);
     log.debug("请求高德路线规划API: {}", drivingUrl);

     try {
         //  调用API
         ResponseEntity<String> response = restTemplate.getForEntity(drivingUrl, String.class);

         // 解析响应
         DirectionResponse directionResponse = parseDirectionResponse(response.getBody());

         if (directionResponse == null) {
             log.warn("高德API返回数据解析失败: {}", response.getBody());
             return  null;
//             throw new RuntimeException("高德地图路线规划数据解析失败");
         }

         return directionResponse;

     } catch (RestClientException e) {
         log.error("调用高德地图API失败", e);
         return  null;
//         throw new RuntimeException("高德地图服务暂时不可用", e);
     }
 }



    /**
     * 解析高德地图API返回的距离分钟
     */
    private DirectionResponse parseDirectionResponse(String jsonResponse) {
        if (StringUtils.isEmpty(jsonResponse)) {
            return null;
        }
        try {
            JSONObject response = JSONObject.parseObject(jsonResponse);

            // 检查API状态
            if (response.getIntValue("status") != 1) {
                log.warn("高德API返回状态异常: {}", jsonResponse);
                return null;
            }

            // 获取路径信息
            JSONObject route = response.getJSONObject(AmapConfigConstants.ROUTE);
            JSONArray paths = route.getJSONArray(AmapConfigConstants.PATHS);

            if (paths == null || paths.isEmpty()) {
                log.warn("高德API返回无路径数据: {}", jsonResponse);
                return null;
            }

            // 解析第一条路径
            JSONObject path = paths.getJSONObject(0);
            return new DirectionResponse()
                    .setDistance(path.getIntValue("distance"))
                    .setDuration(path.getIntValue("duration"));

        } catch (Exception e) {
            log.error("解析高德地图API响应失败: {}", jsonResponse, e);
            return null;
        }
    }

}
